<?php
include_once("dbconnect.php");

$user_id = $connection->query("SELECT userid FROM user WHERE userid LIKE 'U%' ORDER BY userid DESC LIMIT 1");
$user_last_id = $user_id->fetch_assoc()['userid'] ?? 'U0000';

// Extract numeric part and increment it
$user_numeric_part = (int)substr($user_last_id, 1);
$user_new_numeric_part = str_pad($user_numeric_part + 1, 4, '0', STR_PAD_LEFT);
$user_new_id = 'U' . $user_new_numeric_part;


if(isset($_POST['submit'])){
    $username = $_POST['username'];
    $email=$_POST['email'];
    $phoneno=$_POST['phoneno'];
    $password=$_POST['password'];

    $sql = "INSERT INTO  user Values ('$user_new_id','$username','$email','$phoneno','$password')";
    if ($connection->query($sql) === TRUE){
        header("Location:register.php");
        }
    else{
        echo "Error: " . $sql . "<br>" . $connection->error;
        }

}
?>